﻿Public Class ucHeader
    Private FormTarget As Form
    Private FormExit As Form
    Private frmCtrl As New control
    Private dgvBase As DataGridView
    Private dgvKey As String

    Public Event tsNewClick(ByVal sender As ucHeader)
    Public Event tsEditClick(ByVal sender As ucHeader)
    Public Event tsViewClick(ByVal sender As ucHeader)
    Public Event tsDeleteClick(ByVal sender As ucHeader)
    Public Event tsPrintClick(ByVal sender As ucHeader)
    Public Event tsSaveClick(ByVal sender As ucHeader)
    Public Event tsCancelClick(ByVal sender As ucHeader)
    Public Event tsPostingClick(ByVal sender As ucHeader)
    Public Event tsRefreshClick(ByVal sender As ucHeader)

    Public Sub Iniliaze(Title As String, CurrentForm As Form, LastForm As Form, DataGrid As DataGridView, DataGridKey As String, useCMS As Boolean)
        lblTitle.Text = Title
        FormTarget = CurrentForm
        CurrentForm.Text = Title
        FormExit = LastForm
        dgvBase = DataGrid
        dgvKey = DataGridKey
        If (dgvBase Is Nothing) Then
            ActionCtrl(True, False, False, False, False, False, False, True, False, True)
        Else
            If (dgvBase.RowCount > 0 And useCMS = True) Then
                dgvBase.ContextMenuStrip = cmsBase
            End If
        End If
    End Sub

    Public Sub ActionCtrl(N As Boolean, V As Boolean, E As Boolean, D As Boolean, P As Boolean, S As Boolean, C As Boolean, R As Boolean, A As Boolean, X As Boolean)

        tsNew.Visible = N
        tsView.Visible = V
        tsEdit.Visible = E
        tsDelete.Visible = D
        tsPrint.Visible = P
        tsSave.Visible = S
        tsCancel.Visible = C
        tsRefresh.Visible = R
        tsPosting.Visible = A
        tsExit.Visible = X
        If (dgvBase IsNot Nothing) Then
            NewToolStripMenuItem.Enabled = N
            EditToolStripMenuItem.Enabled = E
            DeleteToolStripMenuItem.Enabled = D
            RefreshToolStripMenuItem.Enabled = R
            ExitToolStripMenuItem.Enabled = X
        End If
    End Sub

    Private Sub tsNew_Click(sender As Object, e As EventArgs) Handles tsNew.Click
        RaiseEvent tsNewClick(Me)
        ActionCtrl(False, False, False, False, False, True, True, False, False, True)
    End Sub

    Private Sub tsEdit_Click(sender As Object, e As EventArgs) Handles tsEdit.Click
        RaiseEvent tsEditClick(Me)
        ActionCtrl(False, False, False, False, False, True, True, False, False, True)
    End Sub

    Private Sub tsDelete_Click(sender As Object, e As EventArgs) Handles tsDelete.Click
        If (dgvBase IsNot Nothing) Then
            Dim rowI As Integer = dgvBase.CurrentRow.Index
            Dim answer As DialogResult
            answer = MessageBox.Show("Are you sure deleting data. " + dgvBase.Item(dgvKey, rowI).Value.ToString().Trim() + " ?", "Delete Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If answer = vbYes Then
                RaiseEvent tsDeleteClick(Me)
            End If
        Else
            Dim answer As DialogResult
            answer = MessageBox.Show("Are you sure deleting this data ?", "Delete Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If answer = vbYes Then
                tsDelete_Click(Nothing, Nothing)
            End If
        End If
    End Sub

    Private Sub tsPrint_Click(sender As Object, e As EventArgs) Handles tsPrint.Click
        RaiseEvent tsPrintClick(Me)
    End Sub

    Private Sub tsSave_Click(sender As Object, e As EventArgs) Handles tsSave.Click
        Dim answer As DialogResult
        answer = MessageBox.Show("are you sure?", "Save Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If answer = vbYes Then
            RaiseEvent tsSaveClick(Me)
        End If
    End Sub

    Private Sub tsCancel_Click(sender As Object, e As EventArgs) Handles tsCancel.Click
        Dim answer As DialogResult
        answer = MessageBox.Show("All Change will be not saved, are you sure?", "Cancel Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If answer = vbYes Then
            RaiseEvent tsCancelClick(Me)
        End If
    End Sub

    Private Sub tsView_Click(sender As Object, e As EventArgs) Handles tsView.Click
        RaiseEvent tsViewClick(Me)
    End Sub

    Private Sub tsPosting_Click(sender As Object, e As EventArgs) Handles tsPosting.Click
        RaiseEvent tsPostingClick(Me)
    End Sub

    Private Sub tsExit_Click(sender As Object, e As EventArgs) Handles tsExit.Click
        If (FormExit Is Nothing) Then
            FormTarget.Close()
        Else : frmCtrl.openChild(FormExit, FormTarget.ParentForm)
        End If
    End Sub

    Private Sub tsRefresh_Click(sender As Object, e As EventArgs) Handles tsRefresh.Click
        RaiseEvent tsRefreshClick(Me)
    End Sub

    Private Sub ToolbarHeader_Load(sender As Object, e As EventArgs) Handles Me.Load
        ActionCtrl(True, False, False, False, False, False, False, True, False, True)
    End Sub

    Private Sub cmsBase_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles cmsBase.ItemClicked
        Select Case e.ClickedItem.Text
            Case "&New"
                tsNew_Click(Nothing, Nothing)
            Case "&View"
                tsView_Click(Nothing, Nothing)
            Case "&Edit"
                tsEdit_Click(Nothing, Nothing)
            Case "&Refresh"
                tsRefresh_Click(Nothing, Nothing)
            Case "&Delete"
                tsDelete_Click(Nothing, Nothing)
            Case "&Close"
                FormTarget.Close()
        End Select
    End Sub
End Class
